<?php

namespace App\Api\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StoragesModel extends Model
{
    use HasFactory;

    protected $table = 'storage';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    //仓库列表
    static public function getStorageList($parentId, $data)
    {
        return SELF::select(
            'a.id',
            'storage_name',
            'storage_address',
            'remarks',
            'a.user_id',
            'b.nickname',
            'b.username'
        )
            ->from('storage as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->where('main_id', $parentId)
            ->where(function($query) use ($data) {
                $query->orWhere('storage_name', 'like', '%'.$data['keyword'].'%');
                $query->orWhere('storage_address', 'like', '%'.$data['keyword'].'%');
            })
            ->where('a.delete_flag', 0)
            ->orderBy('a.id', 'DESC')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
    }

    //仓库下拉列表
    static public function getStorageSelect($mainId)
    {
        return SELF::select(
            'id',
            'storage_name',
            'storage_address'
        )
            ->where('main_id', $mainId)
            ->where('delete_flag', 0)
            ->orderBy('id', 'DESC')
            ->get();
    }
}
